package com.tanqin.jobs;

import com.tanqin.constant.RedisConstant;
import com.tanqin.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 自定义 Job，用于定时清理垃圾图片
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        // 根据 Redis 中保存的两个 set 集合进行差值计算，获得垃圾图片名称集合
        Set<String> setDiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        if (setDiff != null) {
            for (String picName : setDiff) {
                QiniuUtils.deleteFileFromQiniu(picName);
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
                System.out.println("删除了垃圾图片：" + picName);
            }
        }
    }
}
